-
Notifications
You must be signed in to change notification settings - Fork 764
Classify questions #6663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Classify questions #6663
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,4 @@ chromedriver.log | |
.cursor-server | ||
.cursor | ||
.gk/ | ||
gcloud/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +0,0 @@ | ||
from functools import cache | ||
|
||
from langchain_google_vertexai import ChatVertexAI | ||
|
||
|
||
@cache | ||
def get_llm( | ||
model_name: str, temperature: int = 1, max_tokens: int | None = None, max_retries: int = 2 | ||
) -> ChatVertexAI: | ||
""" | ||
Returns a LangChain chat model instance based on the given LLM model name. | ||
""" | ||
return ChatVertexAI( | ||
model=model_name, temperature=temperature, max_tokens=max_tokens, max_retries=max_retries | ||
) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from django.db import models | ||
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough | ||
|
||
from kitsune.llm.questions.prompt import spam_parser, spam_prompt, topic_parser, topic_prompt | ||
from kitsune.llm.utils import get_llm | ||
from kitsune.products.utils import get_taxonomy | ||
|
||
DEFAULT_LLM_MODEL = "gemini-2.5-flash-preview-04-17" | ||
HIGH_CONFIDENCE_THRESHOLD = 75 | ||
LOW_CONFIDENCE_THRESHOLD = 60 | ||
|
||
if TYPE_CHECKING: | ||
from kitsune.questions.models import Question | ||
|
||
|
||
class ModerationAction(models.TextChoices): | ||
NOT_SPAM = "not_spam", "Not Spam" | ||
SPAM = "spam", "Spam" | ||
FLAG_REVIEW = "flag_review", "Flag for Review" | ||
|
||
|
||
def classify_question(question: "Question") -> dict[str, Any]: | ||
""" | ||
Analyze a question for spam and, if not spam or low confidence, classify the topic. | ||
Returns a dict with keys: action, spam_result, topic_result (optional). | ||
""" | ||
llm = get_llm(model_name=DEFAULT_LLM_MODEL) | ||
|
||
product = question.product | ||
payload: dict[str, Any] = { | ||
"question": question.content, | ||
"product": product, | ||
"topics": get_taxonomy( | ||
product, include_metadata=["description", "examples"], output_format="JSON" | ||
), | ||
} | ||
|
||
spam_detection_chain = spam_prompt | llm | spam_parser | ||
topic_classification_chain = topic_prompt | llm | topic_parser | ||
|
||
def decision_lambda(payload: dict[str, Any]) -> dict[str, Any]: | ||
spam_result: dict[str, Any] = payload["spam_result"] | ||
confidence: int = spam_result.get("confidence", 0) | ||
is_spam: bool = spam_result.get("is_spam", False) | ||
result = { | ||
"action": ModerationAction.NOT_SPAM, | ||
"spam_result": spam_result, | ||
"topic_result": {}, | ||
} | ||
|
||
if is_spam: | ||
match confidence: | ||
case _ if confidence >= HIGH_CONFIDENCE_THRESHOLD: | ||
result["action"] = ModerationAction.SPAM | ||
case _ if ( | ||
confidence > LOW_CONFIDENCE_THRESHOLD | ||
and confidence < HIGH_CONFIDENCE_THRESHOLD | ||
): | ||
result["action"] = ModerationAction.FLAG_REVIEW | ||
|
||
if result["action"] == ModerationAction.NOT_SPAM: | ||
result["topic_result"] = topic_classification_chain.invoke(payload) | ||
|
||
return result | ||
|
||
pipeline = RunnablePassthrough.assign(spam_result=spam_detection_chain) | RunnableLambda( | ||
decision_lambda | ||
) | ||
result: dict[str, Any] = pipeline.invoke(payload) | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import waffle | ||
from celery import shared_task | ||
|
||
from kitsune.llm.questions.classifiers import classify_question | ||
from kitsune.users.models import Profile | ||
|
||
shared_task_with_retry = shared_task( | ||
acks_late=True, autoretry_for=(Exception,), retry_backoff=2, retry_kwargs=dict(max_retries=3) | ||
) | ||
|
||
|
||
@shared_task_with_retry | ||
def question_classifier(question_id): | ||
from kitsune.questions.models import Question | ||
from kitsune.questions.utils import flag_question, process_classification_result | ||
|
||
try: | ||
question = Question.objects.get(id=question_id) | ||
except Question.DoesNotExist: | ||
return | ||
|
||
if waffle.switch_is_active("auto-question-classifier"): | ||
result = classify_question(question) | ||
process_classification_result(question, result) | ||
elif waffle.switch_is_active("flagit-spam-autoflag"): | ||
flag_question( | ||
question, | ||
by_user=Profile.get_sumo_bot(), | ||
notes=( | ||
"Automatically flagged for topic moderation:" | ||
" auto-question-classifier is disabled" | ||
), | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from functools import cache | ||
|
||
from langchain_google_vertexai import ChatVertexAI | ||
|
||
|
||
@cache | ||
def get_llm( | ||
model_name: str, | ||
temperature: int = 1, | ||
max_tokens: int | None = None, | ||
max_retries: int = 2, | ||
) -> ChatVertexAI: | ||
""" | ||
Returns a LangChain chat model instance based on the given LLM model name. | ||
""" | ||
return ChatVertexAI( | ||
model=model_name, temperature=temperature, max_tokens=max_tokens, max_retries=max_retries | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.